package com.saianfu.flowable.demo.controller

import com.saianfu.flowable.attendence.LeaveType
import com.saianfu.flowable.demo.common.impl.*
import org.flowable.engine.TaskService
import org.flowable.task.api.Task
import org.junit.jupiter.api.Test


import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
internal class ProcessControllerTest {
    @Autowired
    lateinit var processHandler: ProcessHandler

    @Autowired
    lateinit var instanceHandler: InstanceHandler

    @Autowired
    lateinit var histTaskQueryHandler: HistTaskQueryHandler

    @Autowired
    lateinit var taskHandler: TaskHandler

    @Autowired
    lateinit var taskQueryHandler: TaskQueryHandler

    @Autowired
    lateinit var taskService: TaskService

    companion object {
        protected var logger = LoggerFactory.getLogger(InstanceHandler::class.java)
    }

    @Test
    fun deployRepeat() {
        logger.info("部署流程定义start")
        val deploy = processHandler.deploy("processes/Approve001.bpmn20.xml", "Approve001", "Approve001")
        logger.info("部署流程定义end--{}:{}", deploy.id, deploy.name)

//        val variables = mutableMapOf(
//                Pair("skipHR", 0),
//                Pair("skipOffice", 1),
//                Pair("skipDepartment", 1),
//                Pair("skipBranchLeader", 1),
//                Pair("skipViceManager", 0),
//                Pair("skipGeneralManager", 0)
//        )
//
//        val processInstance = instanceHandler.startProcessInstanceByKey("Approve002", variables)
//        println("processInstanceId:${processInstance.processInstanceId}")
//
//        val task: Task = taskService.createTaskQuery().processInstanceId(processInstance.processInstanceId).active().singleResult()
//        println("task:${task.id}")

    }

    @Test
    fun completeTask() {
        val processInstanceId = "6cbe3df3-3b0e-11eb-9bf2-021f08fd9bac"
        val taskList = taskService.createTaskQuery().processInstanceId(processInstanceId)
                .active().list()
        println("completeTask:${taskList.size}")
        taskList.forEach { task ->
            task.assignee = "user001"
            taskService.complete(task.id)
            println("task.id:${task.id}")
        }

    }

    @Test
    fun printLeaveTypeList()
    {
        println(LeaveType.AnnualLeave.code)
        println(LeaveType.AnnualLeave.label)
    }





}